<!DOCTYPE html>
<html lang="zh" xmlns="">
<head>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-dark.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.0.5/purify.min.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 对话助手</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 50%, #16213e 100%);
            color: #e0e0e0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            overflow-x: hidden;
        }

        .chat-container {
            width: 100%;
            height: 100vh;
            margin: 0;
            background: rgba(18, 18, 24, 0.95);
            border-radius: 0;
            padding: 20px;
            box-shadow: none;
            backdrop-filter: blur(10px);
            border: none;
            position: relative;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }

        .chat-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, 
                transparent 0%, 
                #00ffff 25%, 
                #ff00ff 50%, 
                #00ffff 75%, 
                transparent 100%);
            animation: scanline 3s linear infinite;
        }

        @keyframes scanline {
            0% { transform: translateX(-100%); }
            100% { transform: translateX(100%); }
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
        }

        .header h2 {
            font-size: 2.2rem;
            background: linear-gradient(135deg, #00ffff 0%, #ff00ff 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            text-shadow: 0 0 20px rgba(0, 255, 255, 0.3);
            margin-bottom: 10px;
        }

        .header::after {
            content: '';
            display: block;
            width: 100px;
            height: 3px;
            background: linear-gradient(90deg, #00ffff, #ff00ff);
            margin: 10px auto;
            border-radius: 2px;
        }

        #chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            margin-bottom: 25px;
            background: rgba(13, 13, 18, 0.8);
            border-radius: 12px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            scrollbar-width: thin;
            scrollbar-color: rgba(0, 255, 255, 0.3) rgba(13, 13, 18, 0.8);
        }

        #chat-messages::-webkit-scrollbar {
            width: 6px;
        }

        #chat-messages::-webkit-scrollbar-track {
            background: rgba(13, 13, 18, 0.8);
            border-radius: 3px;
        }

        #chat-messages::-webkit-scrollbar-thumb {
            background: linear-gradient(180deg, #00ffff, #ff00ff);
            border-radius: 3px;
        }

        .message {
            margin: 20px 0;
            display: flex;
            align-items: flex-start;
            gap: 15px;
            animation: messageSlide 0.3s ease-out;
        }

        @keyframes messageSlide {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .user {
            flex-direction: row-reverse;
        }

        .avatar {
            width: 45px;
            height: 45px;
            border-radius: 50%;
            border: 2px solid;
            flex-shrink: 0;
            position: relative;
            overflow: hidden;
        }

        .user .avatar {
            border-color: #00ffff;
            box-shadow: 0 0 15px rgba(0, 255, 255, 0.4);
        }

        .bot .avatar {
            border-color: #ff00ff;
            box-shadow: 0 0 15px rgba(255, 0, 255, 0.4);
        }

        .avatar::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            background: conic-gradient(from 0deg, #00ffff, #ff00ff, #00ffff);
            border-radius: 50%;
            z-index: -1;
            animation: rotate 3s linear infinite;
        }

        @keyframes rotate {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .content {
            flex: 1;
            min-width: 0;
            max-width: calc(100% - 70px);
        }

        .nickname {
            font-size: 0.85rem;
            font-weight: 600;
            margin-bottom: 5px;
            display: block;
        }

        .user .nickname {
            color: #00ffff;
            text-align: right;
        }

        .bot .nickname {
            color: #ff00ff;
        }

        .text {
            padding: 15px 18px;
            border-radius: 18px;
            position: relative;
            word-break: break-word;
            white-space: pre-wrap;
            line-height: 1.6;
            font-size: 0.95rem;
        }

        .user .text {
            background: linear-gradient(135deg, rgba(0, 255, 255, 0.15) 0%, rgba(0, 255, 255, 0.05) 100%);
            border: 1px solid rgba(0, 255, 255, 0.2);
            margin-left: 60px;
            border-bottom-right-radius: 4px;
        }

        .bot .text {
            background: linear-gradient(135deg, rgba(255, 0, 255, 0.15) 0%, rgba(255, 0, 255, 0.05) 100%);
            border: 1px solid rgba(255, 0, 255, 0.2);
            margin-right: 60px;
            border-bottom-left-radius: 4px;
        }

        .input-area {
            display: flex;
            gap: 15px;
            align-items: center;
            position: relative;
        }

        #chat-input {
            flex: 1;
            padding: 15px 20px;
            background: rgba(13, 13, 18, 0.8);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-radius: 25px;
            color: #e0e0e0;
            font-size: 1rem;
            transition: all 0.3s ease;
            outline: none;
        }

        #chat-input:focus {
            border-color: #00ffff;
            box-shadow: 0 0 20px rgba(0, 255, 255, 0.3);
            background: rgba(13, 13, 18, 0.9);
        }

        #chat-input::placeholder {
            color: rgba(255, 255, 255, 0.4);
        }

        #send-button {
            padding: 15px 25px;
            background: linear-gradient(135deg, #00ffff 0%, #ff00ff 100%);
            color: #0c0c0c;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-weight: 600;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        #send-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 255, 255, 0.4);
        }

        #send-button:active {
            transform: translateY(0);
        }

        #send-button::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
            transition: left 0.5s;
        }

        #send-button:hover::before {
            left: 100%;
        }

        /* Markdown 样式 */
        .text {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.7;
            color: #e0e0e0;
        }

        .text h1, .text h2, .text h3 {
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            padding-bottom: 0.4em;
            margin-top: 1em;
            margin-bottom: 0.8em;
        }

        .text h1 {
            color: #00ffff;
            font-size: 1.8em;
        }

        .text h2 {
            color: #ff00ff;
            font-size: 1.5em;
        }

        .text h3 {
            color: #e0e0e0;
            font-size: 1.3em;
        }

        .text code {
            background: rgba(0, 255, 255, 0.1);
            padding: 0.2em 0.4em;
            border-radius: 0.3em;
            border: 1px solid rgba(0, 255, 255, 0.2);
            color: #00ffff;
        }

        .text pre {
            background: rgba(13, 13, 18, 0.9);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            padding: 15px;
            overflow-x: auto;
            margin: 15px 0;
        }

        .text blockquote {
            border-left: 4px solid #ff00ff;
            background: rgba(255, 0, 255, 0.05);
            padding: 10px 15px;
            margin: 15px 0;
            border-radius: 0 8px 8px 0;
        }

        .text ul, .text ol {
            padding-left: 25px;
            margin: 10px 0;
        }

        .text li {
            margin: 5px 0;
        }

        .text a {
            color: #00ffff;
            text-decoration: none;
            border-bottom: 1px solid rgba(0, 255, 255, 0.3);
            transition: all 0.3s ease;
        }

        .text a:hover {
            color: #ff00ff;
            border-bottom-color: #ff00ff;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .chat-container {
                margin: 0;
                padding: 15px;
            }

            .header {
                margin-bottom: 20px;
            }

            .header h2 {
                font-size: 1.8rem;
            }

            #chat-messages {
                padding: 15px;
                margin-bottom: 20px;
            }

            .avatar {
                width: 35px;
                height: 35px;
            }

            .text {
                padding: 12px 15px;
                font-size: 0.9rem;
            }

            .user .text {
                margin-left: 45px;
            }

            .bot .text {
                margin-right: 45px;
            }

            .input-area {
                gap: 10px;
            }

            #chat-input {
                padding: 12px 15px;
            }

            #send-button {
                padding: 12px 20px;
            }
        }

        /* 加载动画 */
        .typing-indicator {
            display: flex;
            align-items: center;
            gap: 5px;
            padding: 10px 15px;
            color: rgba(255, 255, 255, 0.6);
            font-style: italic;
        }

        .typing-dot {
            width: 6px;
            height: 6px;
            background: #ff00ff;
            border-radius: 50%;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typing {
            0%, 60%, 100% {
                transform: translateY(0);
                opacity: 0.4;
            }
            30% {
                transform: translateY(-10px);
                opacity: 1;
            }
        }
    </style>
</head>
<body>
<div class="chat-container">
    <div class="header">
        <h2>AI 对话助手</h2>
        <div style="color: rgba(255, 255, 255, 0.6); font-size: 0.9rem;">智能对话 · 科技未来</div>
    </div>
    <div id="chat-messages"></div>
    <div class="input-area">
        <input type="text" id="chat-input" placeholder="输入你的问题...">
        <button id="send-button">发送</button>
    </div>
</div>

<script>
    $('#chat-input').keypress(function(e) {
        if (e.which === 13) {
            $('#send-button').click();
            return false;
        }
    });

    // 滚动到底部
    function scrollToBottom() {
        const container = $('#chat-messages');
        container.scrollTop(container[0].scrollHeight);
    }

    $('#send-button').click(function () {
        const message = $('#chat-input').val();

        // 立即清空输入框
        $('#chat-input').val('');

        // 用户消息（保持原有样式）
        var userMessage = $('<div class="message user"></div>');
        userMessage.append('<img class="avatar" src="/images/user.png" alt="用户头像">');
        userMessage.append(`<div class="content"><span class="nickname">你</span><div class="text">${message}</div></div>`);

        // 机器人消息容器（先创建空容器）
        var botMessage = $('<div class="message bot"></div>');
        botMessage.append('<img class="avatar" src="/images/robot.png" alt="助手头像">');
        botMessage.append('<div class="content"><span class="nickname">助手</span><div class="text"></div></div>');

        // 按顺序添加消息
        $('#chat-messages').append(userMessage, botMessage);

        // 流式处理
        const eventSource = new EventSource(`/ai/generateStream?message=` + message);
        eventSource.onmessage = function (event) {
            botMessage.find('.text').append(event.data);  // 追加到机器人消息内容区
        };

        // 错误处理
        eventSource.onerror = function (err) {
            $('#chat-input').val(''); // 确保网络异常时也清空
            console.error("连接错误:", err);
            eventSource.close();
        };
    });
    function scrollToMessage(element) {
        element.get(0).scrollIntoView({
            behavior: 'smooth',
            block: 'nearest'
        });
    }

    // 在添加消息后调用
    $('#chat-messages').append(userMessage, botMessage);
    scrollToMessage(botMessage);  // 滚动到最新消息

    // 在流式处理部分修改为：
    eventSource.onmessage = function (event) {
        const markdownText = event.data;

        // 1. 安全过滤（使用DOMPurify）
        const cleanHtml = DOMPurify.sanitize(
            marked.parse(markdownText, {
                breaks: true, // 自动换行
                highlight: function(code, lang) {
                    return hljs.highlightAuto(code).value;
                }
            })
        );

        // 2. 增量更新内容
        const textContainer = botMessage.find('.text');
        textContainer.html(cleanHtml); // 使用html()替代append()

        // 3. 重新高亮代码块
        textContainer.find('pre code').each(function(i, block) {
            hljs.highlightElement(block);
        });

        // 4. 保持滚动
        scrollToMessage(botMessage);
    };
</script>

</body>
</html>
